#include <iostream>
#include <string>
using namespace std;
struct TreeNode 
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
 
//class Solution {
//public:
//    void PreOrder(TreeNode* root, string& str)
//    {
//        if (root == nullptr)
//            return;
//        str += '(';
//        str += to_string(root->val);
//        if (!root->left && root->right)
//            str += "()";
//        PreOrder(root->left, str);
//        PreOrder(root->right, str);
//        str += ')';
//    }
//    string tree2str(TreeNode* root) {
//        string retstr;
//        if (root == nullptr)
//            return retstr;
//        retstr = to_string(root->val);
//        if (!root->left && root->right)
//            retstr += "()";
//        PreOrder(root->left, retstr);
//        PreOrder(root->right, retstr);
//        return retstr;
//    }
//};
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    string tree2str(TreeNode* root) {
        string ans;
        ans += (to_string(root->val));
        if (root->left == nullptr && root->right == nullptr) {
            return ans;
        }
        else if (root->left == nullptr) {
            ans += "()(";
            ans += tree2str(root->right);
            ans += ")";
            return ans;
        }
        else if (root->right == nullptr) {
            ans += "(";
            ans += tree2str(root->left);
            ans += ")";
            return ans;
        }
        else {
            ans += "(";
            ans += tree2str(root->left);
            ans += ")(";
            ans += tree2str(root->right);
            ans += ")";
            return ans;
        }
    }
};
int main()
{

    return 0;
}